#include <iostream>
#include <boost/filesystem/path.hpp>
#include <string>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <boost/filesystem/path.hpp>
#include "components/CmdLine/cmdargs.h"
#include "get_arg.h"
using std::string;
using std::ofstream;
using std::ifstream;
using std::fstream;
using std::cerr;
using std::cout;
using boost::filesystem::path;

#define DEFAULT_CODEML_PATH "/share/tools/paml/"

//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{
	// optional help directive
	CmdArgUsage opt_help('h', "help", 	"display this message", &cout);
	
	// required options
	const unsigned REQUIRED = CmdArg::isREQ | CmdArg::isVALREQ;
	CmdArgStr opt_output_prefix	('o', "output_file_prefix", "<string>",
								"path and prefix for output. i.e. /home/richard/mr will"
								"produce /home/richard/mr_ka.data , mr_ks.data and "
								"mr_kaks.data",
								REQUIRED);
	CmdArgStr opt_errors	('e', "errors_file", "<string>",
								"file listing errors. "
								"Defaults to kaks.errors");
	CmdArgSet opt_yn00		('y', "yn00", "Use yn00 rather than codeml.");
	CmdArgFloat opt_kappa	('k', "kappa", "<number>", "Use a fixed kappa for all "
								"sequences. Only applicable to codeml\n");
	CmdArgStr opt_paml		('p', "paml_path", "<string>",
								"path to codeml or yn00. "
								"Defaults to " DEFAULT_CODEML_PATH "codeml or "
									DEFAULT_CODEML_PATH "yn00.");

	
	CmdArgStr opt_tempdir	('t', "temp_directory", "<string>",
								"temp directory where paml does its work.");
	CmdArgSet opt_verbose	('v', "verbose",
								"issue lots more messages.");
	opt_kappa = -1.0;
	
	string exe_file_name =  boost::filesystem::path(*argv).leaf();

	
	// construct command line
	CmdLine  cmdline(exe_file_name.c_str(),
							&opt_output_prefix,
							&opt_yn00,
							&opt_paml,
							&opt_errors,
							&opt_tempdir,
							&opt_kappa,
							0);
	cmdline.description(
		"This programme calculates the all against all KaKs for a set of homologs.\n\n"
		"A tab delimited file in the following format \"name<tab>cdna_sequence\" "
		"needs to be supplied from STDIN.\n"
		"The resulting output is sent to STDOUT");
	cmdline.set(CmdLine::NO_ABORT | CmdLine::QUIET);

	// parse command line
	CmdArgvIter  argv_iter(--argc, ++argv);

	// report command line errors
	if (cmdline.parse(argv_iter))
		return false;


	
	args.codeml		= !opt_yn00;
	
	args.paml		= opt_paml;
	if (! args.paml.length())
	{
		if (args.codeml)
			args.paml= DEFAULT_CODEML_PATH "codeml";
		else
			args.paml= DEFAULT_CODEML_PATH "yn00";
	}
	
	args.error_file = opt_errors;
	if (!args.error_file.length())
		args.error_file = "kaks.errors";
	
	
	args.tempdir	= opt_tempdir;
	args.output_prefix = opt_output_prefix;
	
	args.kappa = opt_kappa;
	args.fixed_kappa = (args.kappa > 0.0);
	if (args.kappa < 0.0)
		args.kappa= 1.0;
	return true;
}






